//
//  LogSelectView.m
//  StationManager
//
//  Created by 张佳强 on 18/5/12.
//  Copyright © 2018年 Helong. All rights reserved.
//

#import "LogSelectView.h"
#import <Masonry.h>
#import "AFNwebManager.h"
#import "CityModel.h"
@implementation LogSelectView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame{
    
    
    self = [super initWithFrame:frame];
    if (self) {
        
        
        self.backgroundColor = [UIColor whiteColor];
        _taskTypeArray = @[@"巡检",@"紧急"];
        _cityID = @"";
        [self getCityData];
        
        _stationNameLabel = [[UILabel alloc] init];
        _stationNameLabel.text = @"台站名称：";
        _stationNameLabel.font = [UIFont systemFontOfSize:13];
        [self addSubview:_stationNameLabel];
        
        [_stationNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@10);
            make.top.equalTo(@5);
            make.width.mas_equalTo((SCREEN_WIDTH-25)/5);
            make.height.mas_equalTo(30);
            
        }];
        
        _stationNameTF = [[UITextField alloc] init];
        _stationNameTF.borderStyle = UITextBorderStyleRoundedRect;
        _stationNameTF.font = [UIFont systemFontOfSize:13];
        _stationNameTF.textColor = [UIColor lightGrayColor];
        [self addSubview:_stationNameTF];
        
        [_stationNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_stationNameLabel.mas_right).offset(5);
            make.top.equalTo(@5);
            make.right.equalTo(@-10);
            make.height.mas_equalTo(30);
        }];
        
        _taskTypeLabel = [[UILabel alloc] init];
        _taskTypeLabel.font = [UIFont systemFontOfSize:13];
        _taskTypeLabel.text = @"任务类别：";
        [self addSubview:_taskTypeLabel];
        
        [_taskTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@10);
            make.top.equalTo(_stationNameLabel.mas_bottom).offset(5);
            make.width.mas_equalTo((SCREEN_WIDTH-35)/5);
            make.height.mas_equalTo(30);
        }];
        
        _taskTypeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_taskTypeButton setTitle:@"全部" forState:UIControlStateNormal];
        [_taskTypeButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _taskTypeButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _taskTypeButton.layer.masksToBounds = YES;
        _taskTypeButton.layer.cornerRadius = 5;
        _taskTypeButton.layer.borderWidth = 1;
        _taskTypeButton.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0].CGColor;
        [_taskTypeButton addTarget:self action:@selector(taskTypeClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_taskTypeButton];
        
        [_taskTypeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_taskTypeLabel.mas_right).offset(5);
            make.top.equalTo(_stationNameLabel.mas_bottom).offset(5);
            make.width.mas_equalTo((SCREEN_WIDTH-(35+(SCREEN_WIDTH-35)*2/5))/2);
            make.height.mas_equalTo(30);
        }];
        
        _cityLabel = [[UILabel alloc] init];
        _cityLabel.font = [UIFont systemFontOfSize:13];
        _cityLabel.text = @"所属市县：";
        _cityLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_cityLabel];
        
        [_cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_taskTypeButton.mas_right).offset(5);
            make.top.equalTo(_stationNameLabel.mas_bottom).offset(5);
            make.width.mas_equalTo((SCREEN_WIDTH-35)/5);
            make.height.mas_equalTo(30);
        }];
        
        _cityButton = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *location = [USER_DEFAULT objectForKey:@"UserLocation"];
        if ([location isEqualToString:@"石家庄"]) {
            [_cityButton setTitle:@"全部" forState:UIControlStateNormal];

        }
        else{
            [_cityButton setTitle:location forState:UIControlStateNormal];

        }
        [_cityButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _cityButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _cityButton.layer.masksToBounds = YES;
        _cityButton.layer.cornerRadius = 5;
        _cityButton.layer.borderWidth = 1;
        
        _cityButton.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0].CGColor;
        [_cityButton addTarget:self action:@selector(cityClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cityButton];
        
        [_cityButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_cityLabel.mas_right).offset(5);
            make.top.equalTo(_stationNameLabel.mas_bottom).offset(5);
            make.right.equalTo(@-10);
            make.height.mas_equalTo(30);
        }];
        
        
        _startDateLabel = [[UILabel alloc] init];
        _startDateLabel.font = [UIFont systemFontOfSize:13];
        _startDateLabel.text = @"开始时间：";
        [self addSubview:_startDateLabel];
        
        [_startDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@10);
            make.top.equalTo(_taskTypeLabel.mas_bottom).offset(5);
            make.width.mas_equalTo((SCREEN_WIDTH-35)/5);
            make.height.mas_equalTo(30);
        }];
        
        _startDateTF = [[UITextField alloc] init];
        _startDateTF.borderStyle = UITextBorderStyleRoundedRect;
        _startDateTF.text = [self getStartTime];
        _startDateTF.font = [UIFont systemFontOfSize:13];
        _startDateTF.textColor = [UIColor lightGrayColor];
        self.datePicker = [[UIDatePicker alloc]init];
        self.datePicker.datePickerMode = UIDatePickerModeDate;
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"yyyy-MM-dd";
        self.datePicker.date = [formatter dateFromString:_startDateTF.text];
        [self.datePicker addTarget:self action:@selector(dateChange) forControlEvents:UIControlEventValueChanged];
        
        UIToolbar * tool = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        UIBarButtonItem * done = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(doneClick)];
        UIBarButtonItem * space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        NSArray * array = @[space,done];
        [tool setItems:array];
        [self.startDateTF setInputAccessoryView:tool];
        
        self.startDateTF.inputView = self.datePicker;

        [self addSubview:_startDateTF];
        
        [_startDateTF mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_startDateLabel.mas_right).offset(5);
            make.top.equalTo(_taskTypeLabel.mas_bottom).offset(5);
            make.width.mas_equalTo((SCREEN_WIDTH-(35+(SCREEN_WIDTH-35)*2/5))/2);
            make.height.mas_equalTo(30);
        }];
        
        _endDateLabel = [[UILabel alloc] init];
        _endDateLabel.font = [UIFont systemFontOfSize:13];
        _endDateLabel.text = @"结束时间：";
        _endDateLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_endDateLabel];
        
        [_endDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_startDateTF.mas_right).offset(5);
            make.top.equalTo(_taskTypeLabel.mas_bottom).offset(5);
            make.width.mas_equalTo((SCREEN_WIDTH-35)/5);
            make.height.mas_equalTo(30);
        }];
        
        _endDateTF = [[UITextField alloc] init];
        _endDateTF.borderStyle = UITextBorderStyleRoundedRect;
        _endDateTF.text = [self getEndTime];
        _endDateTF.font = [UIFont systemFontOfSize:13];
        _endDateTF.textColor = [UIColor lightGrayColor];
        [_endDateTF setInputAccessoryView:tool];
        self.datePicker2 = [[UIDatePicker alloc]init];
        self.datePicker2.datePickerMode = UIDatePickerModeDate;
        self.datePicker2.date = [formatter dateFromString:_endDateTF.text];
        [self.datePicker2 addTarget:self action:@selector(dateChange2) forControlEvents:UIControlEventValueChanged];
        _endDateTF.inputView = self.datePicker2;
        [self addSubview:_endDateTF];
        
        [_endDateTF mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_endDateLabel.mas_right).offset(5);
            make.top.equalTo(_taskTypeLabel.mas_bottom).offset(5);
            make.right.equalTo(@-10);
            make.height.mas_equalTo(30);
        }];
        
        
        _threeDaysButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_threeDaysButton setTitle:@"近三天" forState:UIControlStateNormal];
        [_threeDaysButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _threeDaysButton.titleLabel.font = [UIFont systemFontOfSize:13];
        _threeDaysButton.selected = NO;
        _threeDaysButton.layer.masksToBounds = YES;
        _threeDaysButton.layer.borderWidth = 1;
        _threeDaysButton.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0].CGColor;
        [_threeDaysButton addTarget:self action:@selector(threeDaysClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_threeDaysButton];
        
        [_threeDaysButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(@10);
            make.bottom.equalTo(@-10);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(30);
        }];
        
        
        _sevenDaysButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sevenDaysButton setTitle:@"近七天" forState:UIControlStateNormal];
        _sevenDaysButton.titleLabel.font = [UIFont systemFontOfSize:13];

        [_sevenDaysButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _sevenDaysButton.selected = NO;
        _sevenDaysButton.layer.masksToBounds = YES;
        _sevenDaysButton.layer.borderWidth = 1;
        _sevenDaysButton.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0].CGColor;
        [_sevenDaysButton addTarget:self action:@selector(sevenDaysClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_sevenDaysButton];
        
        [_sevenDaysButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(_threeDaysButton.mas_right).offset(10);
            make.bottom.equalTo(@-10);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(30);
        }];
        
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchButton setTitle:@"查询" forState:UIControlStateNormal];
        _searchButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_searchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _searchButton.layer.masksToBounds = YES;
        _searchButton.layer.borderWidth = 1;
        _searchButton.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0].CGColor;
        [_searchButton addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_searchButton];
        
        [_searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.width.mas_equalTo(60);
            make.bottom.equalTo(@-10);
            make.right.equalTo(@-10);
            make.height.mas_equalTo(30);
        }];
        
        
    }
    
    return self;
    
}
//点击键盘上的完成按钮
- (void)doneClick {
    
    [self.startDateTF resignFirstResponder];
    [self.endDateTF resignFirstResponder];
}

//时间选择器的值的变化事件
- (void)dateChange{
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString * date = [formatter stringFromDate:self.datePicker.date];
    self.startDateTF.text = date;
}

//时间选择器的值的变化事件
- (void)dateChange2{
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString * date = [formatter stringFromDate:self.datePicker2.date];
    self.endDateTF.text = date;
}



-(void)taskTypeClick{
    
    if (_taskTypeTableView) {
        
        [_taskTypeTableView removeFromSuperview];
    }
    
    _taskTypeTableView = [[UITableView alloc] init];
    _taskTypeTableView.delegate = self;
    _taskTypeTableView.dataSource = self;
    [self addSubview:_taskTypeTableView];
    
    [_taskTypeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_taskTypeButton.mas_left);
        make.top.equalTo(_taskTypeButton.mas_bottom);
        make.right.equalTo(_taskTypeButton.mas_right);
        make.height.mas_equalTo(60);
    }];
    
}


-(void)getCityData{
    
    _cityArray = [NSMutableArray arrayWithCapacity:0];
    
    [[AFNwebManager shareAFNWebManager] SOAPData:nil soapMethod:@"LogListXian" success:^(id responseObject) {
        
//        NSLog(@"%@",responseObject);
        for (NSDictionary *dict in responseObject[@"data"]) {
            CityModel *model = [[CityModel alloc] init];
            model.cityID = dict[@"AutoStationNum"];
            model.cityName = dict[@"Xian"];
            [_cityArray addObject:model];
        }
    } failure:^{
        
    }];
}

-(void)cityClick{
    
//    if (_cityTableView) {
//        
//        [_cityTableView removeFromSuperview];
//    }
//    
//    _cityTableView = [[UITableView alloc] init];
//    _cityTableView.delegate = self;
//    _cityTableView.dataSource = self;
//    [self addSubview:_cityTableView];
//    
//    [_cityTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.left.equalTo(_cityButton.mas_left);
//        make.top.equalTo(_cityButton.mas_bottom);
//        make.right.equalTo(_cityButton.mas_right);
//        make.height.mas_equalTo(300);
//    }];
    
    _pickerView = [[ValuePickerView alloc]init];
    NSMutableArray *cityNameArray = [NSMutableArray arrayWithCapacity:0];
    NSString *location = [USER_DEFAULT objectForKey:@"UserLocation"];
    if ([location isEqualToString:@"石家庄"]) {
        
        for (CityModel *model in self.cityArray) {
            [cityNameArray addObject:model.cityName];
        }

    }
    else{
        
        for (CityModel *model in self.cityArray) {
            if ([model.cityName containsString:location]) {
                
                [cityNameArray addObject:model.cityName];

            }
        }
    }
    
    _pickerView.dataSource = cityNameArray;
    _pickerView.pickerTitle = @"所属市县";
    __weak typeof(self) weakSelf = self;
    _pickerView.valueDidSelect = ^(NSString *value){
        NSArray * stateArr = [value componentsSeparatedByString:@"/"];
        [weakSelf.cityButton setTitle:stateArr[0] forState:UIControlStateNormal];
        for (CityModel *model in weakSelf.cityArray) {
            
            if ([model.cityName isEqualToString:stateArr[0]]) {
                
                _cityID = model.cityID;
            }
        }
    };
    [_pickerView show];

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
        return _taskTypeArray.count;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        static NSString *iden = @"taskCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        }
        
        cell.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        cell.textLabel.text = _taskTypeArray[indexPath.row];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 30;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [_taskTypeButton setTitle:_taskTypeArray[indexPath.row] forState:UIControlStateNormal];
    [_taskTypeTableView removeFromSuperview];
 
}

-(void)threeDaysClick{
    
    _sevenDaysButton.selected = NO;
    [_sevenDaysButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    _sevenDaysButton.layer.masksToBounds = YES;
    _sevenDaysButton.layer.borderWidth = 1;
    _sevenDaysButton.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0].CGColor;
    
    _threeDaysButton.selected = !_threeDaysButton.selected;
    if (_threeDaysButton.selected) {
        
        [_threeDaysButton setTitleColor:[UIColor colorWithRed:34/255.0 green:150/255.0 blue:243/255.0 alpha:1.0] forState:UIControlStateNormal];
        _threeDaysButton.layer.masksToBounds = YES;
        _threeDaysButton.layer.borderWidth = 1;
        _threeDaysButton.layer.borderColor = [[UIColor colorWithRed:34/255.0 green:150/255.0 blue:243/255.0 alpha:1.0] CGColor];
        
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
        //    [lastMonthComps setYear:1]; // year = 1表示1年后的时间 year = -1为1年前的日期，month day 类推
        [lastMonthComps setDay:-3];
        NSDate *newdate = [calendar dateByAddingComponents:lastMonthComps toDate:date options:0];
        //    NSString * startTime = [formatter stringFromDate:newdate];
        
        NSString * realTime2 = [formatter stringFromDate:newdate];
        _startDateTF.text = realTime2;
        self.datePicker.date = [formatter dateFromString:_startDateTF.text];

    }
    else{
        [_threeDaysButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _threeDaysButton.layer.masksToBounds = YES;
        _threeDaysButton.layer.borderWidth = 1;
        _threeDaysButton.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0].CGColor;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        _startDateTF.text = [self getStartTime];
        self.datePicker.date = [formatter dateFromString:_startDateTF.text];

    }
}

-(void)sevenDaysClick{
    
    _threeDaysButton.selected = NO;
    [_threeDaysButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    _threeDaysButton.layer.masksToBounds = YES;
    _threeDaysButton.layer.borderWidth = 1;
    _threeDaysButton.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0].CGColor;
    
    _sevenDaysButton.selected = !_sevenDaysButton.selected;
    if (_sevenDaysButton.selected) {
        
        [_sevenDaysButton setTitleColor:[UIColor colorWithRed:34/255.0 green:150/255.0 blue:243/255.0 alpha:1.0] forState:UIControlStateNormal];
        _sevenDaysButton.layer.masksToBounds = YES;
        _sevenDaysButton.layer.borderWidth = 1;
        _sevenDaysButton.layer.borderColor = [[UIColor colorWithRed:34/255.0 green:150/255.0 blue:243/255.0 alpha:1.0] CGColor];
        
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
        //    [lastMonthComps setYear:1]; // year = 1表示1年后的时间 year = -1为1年前的日期，month day 类推
        [lastMonthComps setDay:-7];
        NSDate *newdate = [calendar dateByAddingComponents:lastMonthComps toDate:date options:0];
        //    NSString * startTime = [formatter stringFromDate:newdate];
        
        NSString * realTime2 = [formatter stringFromDate:newdate];
        _startDateTF.text = realTime2;
        self.datePicker.date = [formatter dateFromString:_startDateTF.text];
    }
    else{
        [_sevenDaysButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        _sevenDaysButton.layer.masksToBounds = YES;
        _sevenDaysButton.layer.borderWidth = 1;
        _sevenDaysButton.layer.borderColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0].CGColor;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"YYYY-MM-dd"];
        _startDateTF.text = [self getStartTime];
        self.datePicker.date = [formatter dateFromString:_startDateTF.text];
    }
    
}

-(void)searchClick{
    

    if ([_cityID isEqualToString:@""]) {
        
        _cityID = @"全部";
    }
    
    [self.logSelectDelegate sendStationName:_stationNameTF.text type:_taskTypeButton.currentTitle city:_cityID startTime:_startDateTF.text endTime:_endDateTF.text];
}

- (NSString *)getStartTime{
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *lastMonthComps = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    //    [lastMonthComps setYear:1]; // year = 1表示1年后的时间 year = -1为1年前的日期，month day 类推
    NSLog(@"year:%zi",lastMonthComps.year);
    //    [lastMonthComps setMonth:-1];
    lastMonthComps.month = lastMonthComps.month-1;
    lastMonthComps.day = 1;
    NSDate *newdate = [calendar dateFromComponents:lastMonthComps];
    //    NSString * startTime = [formatter stringFromDate:newdate];
    
    NSString * realTime2 = [formatter stringFromDate:newdate];
    return realTime2;
}
- (NSString *)getEndTime{
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    //    NSString * endTIme = [formatter stringFromDate:date];
    
    NSString * realTime2 = [formatter stringFromDate:date];
    return realTime2;
}

@end
